<html>
<head><meta charset="utf-8"><title>Where does the unit type come from when types are ambiguous? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html">Where does the unit type come from when types are ambiguous?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227605683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227605683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227605683">(Feb 24 2021 at 14:26)</a>:</h4>
<p>I get that the type of <code>s2</code> is ambiguous here, but why do the error messages assume that it's the unit type instead of saying that it doesn't know what type it is?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">func</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">std</span>::<span class="n">error</span>::<span class="n">Error</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">s2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"1"</span><span class="p">.</span><span class="n">parse</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">s2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0277]: cannot multiply `()` by `i32`
 --&gt; src/lib.rs:4:11
  |
4 |     Ok(s2 * 3)
  |           ^ no implementation for `() * i32`
  |
  = help: the trait `Mul&lt;i32&gt;` is not implemented for `()`

error[E0277]: the trait bound `(): FromStr` is not satisfied
 --&gt; src/lib.rs:2:18
  |
2 |     let s2 = &quot;1&quot;.parse()?;
  |                  ^^^^^ the trait `FromStr` is not implemented for `()`
</code></pre></div>



<a name="227608759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227608759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227608759">(Feb 24 2021 at 14:45)</a>:</h4>
<p>I think this is planned to change as part of <a href="https://github.com/rust-lang/rust/issues/35121">https://github.com/rust-lang/rust/issues/35121</a>. I don't know what the original reasoning for it was.</p>



<a name="227609291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227609291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227609291">(Feb 24 2021 at 14:49)</a>:</h4>
<p>I was worried someone was going to mention fallback. I've read those in passing, but never really attempted to dive in deep:</p>
<p><a href="user_uploads/4715/-ICQ3XztTNeye3Bwq93Xd92B/image.png">image.png</a></p>
<div class="message_inline_image"><a href="user_uploads/4715/-ICQ3XztTNeye3Bwq93Xd92B/image.png" title="image.png"><img src="user_uploads/4715/-ICQ3XztTNeye3Bwq93Xd92B/image.png"></a></div>



<a name="227609352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227609352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227609352">(Feb 24 2021 at 14:49)</a>:</h4>
<p>Lol</p>



<a name="227609465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227609465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227609465">(Feb 24 2021 at 14:50)</a>:</h4>
<p>FWIW I don't think changing fallback to ! would actually help in this case, ! doesn't implement all traits</p>



<a name="227709885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227709885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227709885">(Feb 25 2021 at 02:44)</a>:</h4>
<p>How feasible would it be to not use the fallback if the fallback doesn't actually help? If you get an error either way it seems like it would be better to get an inference error about a metavariable than about a fallback type</p>



<a name="227710864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227710864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227710864">(Feb 25 2021 at 03:01)</a>:</h4>
<p>Actually, if <code>!</code> did implement all traits, as some in that thread proposed, then the above code would work and would probably be a bug (assuming that <code>impl FromStr for !</code> exists and always fails)</p>



<a name="227711601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227711601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227711601">(Feb 25 2021 at 03:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F/near/227710864">said</a>:</p>
<blockquote>
<p>Actually, if <code>!</code> did implement all traits, as some in that thread proposed, then the above code would work and would probably be a bug (assuming that <code>impl FromStr for !</code> exists and always fails)</p>
</blockquote>
<p>that's unlikely to happen though <a href="https://github.com/rust-lang/rust/blob/07194ffcd25b0871ce560b9f702e52db27ac9f77/compiler/rustc_interface/src/util.rs#L675-L682">https://github.com/rust-lang/rust/blob/07194ffcd25b0871ce560b9f702e52db27ac9f77/compiler/rustc_interface/src/util.rs#L675-L682</a></p>



<a name="227721016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Where%20does%20the%20unit%20type%20come%20from%20when%20types%20are%20ambiguous%3F/near/227721016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Where.20does.20the.20unit.20type.20come.20from.20when.20types.20are.20ambiguous.3F.html#227721016">(Feb 25 2021 at 05:41)</a>:</h4>
<p>I mean to use this as an example for why <code>!</code> implementing all traits is a bad idea. It's similar in spirit to the idea that all ZSTs should implement Default - just because it is possible to give an implementation, that implementation may not meet the constraints of the trait/type</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>